<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Inviter
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: AjaxController.php 2010-07-02 19:54 mirlan $
 * @author     Mirlan
 */

/**
 * @category   Application_Extensions
 * @package    Inviter
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Inviter_AjaxController extends Core_Controller_Action_Standard {
	public function init() {
		$XmlRequest = new Zend_Controller_Request_Http ();
		if (! $XmlRequest->isXmlHttpRequest ()) {
			header ( 'location: notfound' );
		}
	}
	
	public function suggestAction() {
		$noneFriend_id = $this->_getParam ( 'nonefriend_id', 0 );
		$currents = $this->_getParam ( 'current_suggests', array () );
		$widget = $this->_getParam ( 'widget' );
		
		unset ( $currents [$noneFriend_id] );
		
		$current_user_ids = array ();
		foreach ( $currents as $current ) {
			$current_user_ids [] = $current ['user_id'];
		}
		$current_user_ids = implode ( ',', $current_user_ids );
		
		$suggest_array = Engine_Api::_ ()->getDbtable ( 'nonefriends', 'inviter' )->getSuggests ( array ('current_suggests' => $current_user_ids, 'noneFriend_id' => $noneFriend_id, 'total_suggests' => 1 ) );
		
		$suggest = $suggest_array ['suggests'];
		
		if ($suggest instanceof User_Model_User) {
			$currents [$suggest->getIdentity ()] = $suggest->getIdentity ();
			
			$mutualFriends = $suggest_array ['mutual_friends'] [$suggest->getIdentity ()];
			
			$content = $this->view->suggest ( array ('user' => $suggest, 'mutual_friends' => explode ( ',', $mutualFriends ), 'widget' => $widget ) );
		} else {
			$content = '';
		}
		
		$this->view->current_suggests = $currents;
		$this->view->html = Zend_Json::encode ( $content );
	}
	
	public function addtosuggestAction() {
		$viewer = $this->_helper->api ()->user ()->getViewer ();
		$user_ids = $this->_getParam ( 'user_ids' );
		
		if (! $viewer->getIdentity () && count ( $user_ids ) == 0) {
			$this->view->result = 0;
			return;
		}
		
		$table = Engine_Api::_ ()->getDbtable ( 'nonefriends', 'inviter' );
		$select = $table->select ()->where ( 'user_id = ?', $viewer->getIdentity () )->limit ( 1 );
		
		$nonfriendsRow = $table->fetchRow ( $select );
		
		$nonfriend_ids = $nonfriendsRow->nonefriend_ids;
		$nonfriend_ids = explode ( ',', $nonfriend_ids );
		
		$nonfriends = array ();
		foreach ( $nonfriend_ids as $nonfriend_id ) {
			$nonfriends [$nonfriend_id] = $nonfriend_id;
		}
		
		foreach ( $user_ids as $user_id ) {
			unset ( $nonfriends [$user_id] );
		}
		
		if (count ( $nonfriends ) > 0) {
			$nonfriends = array_unique ( $nonfriends );
			$nonfriends = implode ( ',', $nonfriends );
			
			$nonfriendsRow->nonefriend_ids = $nonfriends;
			$nonfriendsRow->save ();
		} else {
			$nonfriendsRow->delete ();
		}
		
		$this->view->result = 1;
	}
}